package me.zhengjie.modules.website.repository;

import me.zhengjie.modules.website.domain.ColumnTable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @author system
 * @since 2024-09-26 16:06:29
 */
@Component
public class ColumnTableSpecifications {

    public static Specification<ColumnTable> queryList(ColumnTable columnTable) {
        return (root, query, builder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (columnTable.getColumnId() != null) {
                predicates.add(builder.like(root.get("columnId" ).as(String.class), "%" + columnTable.getColumnId() + "%" ));
            }
            if (columnTable.getColumnTitle() != null) {
                predicates.add(builder.like(root.get("columnTitle" ).as(String.class), "%" + columnTable.getColumnTitle() + "%" ));
            }
            if (columnTable.getColumnType() != null) {
                predicates.add(builder.like(root.get("columnType" ).as(String.class), "%" + columnTable.getColumnType() + "%" ));
            }
            if (columnTable.getColumnDescription() != null) {
                predicates.add(builder.like(root.get("columnDescription" ).as(String.class), "%" + columnTable.getColumnDescription() + "%" ));
            }
            if (columnTable.getStatus() != null) {
                predicates.add(builder.like(root.get("status" ).as(String.class), "%" + columnTable.getStatus() + "%" ));
            }
            if (columnTable.getSortOrder() != null) {
                predicates.add(builder.equal(root.get("sortOrder" ).as(Integer.class), columnTable.getSortOrder()));
            }
            if (columnTable.getColumnLevel() != null) {
                predicates.add(builder.equal(root.get("columnLevel" ).as(Integer.class), columnTable.getColumnLevel()));
            }
            if (columnTable.getOpenMethod() != null) {
                predicates.add(builder.like(root.get("openMethod" ).as(String.class), "%" + columnTable.getOpenMethod() + "%" ));
            }
            if (columnTable.getParentColumnId() != null) {
                predicates.add(builder.equal(root.get("parentColumnId" ).as(Integer.class), columnTable.getParentColumnId()));
            }
            Predicate[] p = new Predicate[predicates.size()];
            return query.where(builder.and(predicates.toArray(p))).getRestriction();
        };
    }

}
